#include "Delay.h"


static TIM_HandleTypeDef* delay_timer = &htim2;

/**
  * @brief ????????
  */
void timer_delay_init(void)
{
    HAL_TIM_Base_Start(delay_timer);
    __HAL_TIM_SET_COUNTER(delay_timer, 0);
}

/**
  * @brief ????
  * @param us: ??? (1-32767)
  */
void delay_us(uint32_t us)
{
    uint32_t start = __HAL_TIM_GET_COUNTER(delay_timer);
    uint32_t ticks = us * 1;  // TIM2????1MHz,1us??1?tick
    
    // ???????(??)???
    while ((__HAL_TIM_GET_COUNTER(delay_timer) - start) < ticks) {
        if (__HAL_TIM_GET_COUNTER(delay_timer) < start) {
            start = __HAL_TIM_GET_COUNTER(delay_timer);
        }
    }
}